package com.ityang.hhhh.utils;


import com.alibaba.druid.support.json.JSONUtils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class XinWen {



        //java环境中文传值时，需特别注意字符编码问题
      public static  final  String  httpUrl = "http://api.tianapi.com/generalnews/index?key=3d0ebeaae433afcfcbc0d092c81f2401&num=10&page=0&rand=1";
        //String jsonResult = request(httpUrl);

       // System.out.println(jsonResult);


        public static String request (String httpUrl, String httpArg){
            BufferedReader reader = null;
            String result = null;
            StringBuffer sbf = new StringBuffer();
            httpUrl = httpUrl + "&" + httpArg;

            try {
                URL url = new URL(httpUrl+ URLEncoder.encode("","utf-8"));
                HttpURLConnection connection = (HttpURLConnection) url
                        .openConnection();
                connection.setRequestMethod("GET");
                InputStream is = connection.getInputStream();
                reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                String strRead = null;
                while ((strRead = reader.readLine()) != null) {
                    sbf.append(strRead);
                    sbf.append("\r\n");
                }
                reader.close();
                result = sbf.toString();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        }

}
